package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"time"

	"github.com/segmentio/kafka-go"
)

func main() {
	ctx := context.Background()
	// 初始化 Kafka 写入器
	w := &kafka.Writer{
		Addr:     kafka.TCP(config.Kafka.Server),
		Topic:    config.Kafka.Topic,
		Balancer: &kafka.LeastBytes{},
	}
	defer w.Close()

	// 模拟用户关注操作
	followerID := 1
	followedID := 2

	// 构造消息
	message := FollowMessage{
		Action:     "user_follow",
		FollowerID: followerID,
		FollowedID: followedID,
	}
	messageBytes, err := json.Marshal(message)
	if err != nil {
		log.Fatalf("Failed to marshal message: %v", err)
	}

	// 发送消息到 Kafka
	err = w.WriteMessages(ctx, kafka.Message{Value: messageBytes})
	if err != nil {
		log.Fatalf("Failed to send message to Kafka: %v", err)
	}

	fmt.Printf("User %d followed user %d, message sent to Kafka.\n", followerID, followedID)
	// 给 Kafka 一些时间处理消息
	time.Sleep(2 * time.Second)
}
